<template>
  <div class="forgot-password-container">
    <div class="forgot-password-card">
      <h2 class="title">重置密码</h2>
      <p class="subtitle">请输入您的邮箱地址，我们将发送重置密码的链接到您的邮箱</p>
      
      <el-form 
        ref="formRef"
        :model="form" 
        :rules="rules"
        @submit.prevent="handleSubmit"
        class="forgot-form"
      >
        <el-form-item prop="email">
          <el-input 
            v-model="form.email" 
            type="email"
            size="large"
            placeholder="请输入邮箱地址"
            prefix-icon="Message"
            @keyup.enter="handleSubmit"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            size="large"
            :loading="loading"
            @click="handleSubmit"
            class="submit-button"
          >
            {{ loading ? '发送中...' : '发送重置链接' }}
          </el-button>
        </el-form-item>
        
        <el-form-item>
          <div class="back-link">
            <router-link to="/login">返回登录</router-link>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'

// 表单引用
const formRef = ref<FormInstance>()

// 表单数据
const form = reactive({
  email: ''
})

// 表单验证规则
const rules = reactive<FormRules>({
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入有效的邮箱地址', trigger: ['blur', 'change'] }
  ]
})

// 加载状态
const loading = ref(false)

// 处理提交
const handleSubmit = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (!valid) return
    
    loading.value = true
    try {
      // TODO: 调用忘记密码API
      await new Promise(resolve => setTimeout(resolve, 2000)) // 模拟API调用
      
      ElMessage.success('重置密码链接已发送到您的邮箱，请查收')
      
      // 重置表单
      form.email = ''
    } catch (error: any) {
      ElMessage.error(error.message || '发送失败，请稍后再试')
    } finally {
      loading.value = false
    }
  })
}
</script>

<style scoped>
.forgot-password-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.forgot-password-card {
  background: white;
  padding: 40px;
  border-radius: 10px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
  text-align: center;
}

.title {
  margin-bottom: 16px;
  color: #303133;
  font-size: 24px;
  font-weight: 500;
}

.subtitle {
  margin-bottom: 30px;
  color: #606266;
  font-size: 14px;
  line-height: 1.5;
}

.forgot-form {
  width: 100%;
}

.submit-button {
  width: 100%;
}

.back-link {
  text-align: center;
  color: #606266;
  font-size: 14px;
}

.back-link a {
  color: #409eff;
  text-decoration: none;
}

.back-link a:hover {
  text-decoration: underline;
}

:deep(.el-input__wrapper) {
  padding: 8px 15px;
}

:deep(.el-form-item) {
  margin-bottom: 22px;
}
</style>